package com.mycc.game.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mycc.game.common.Code;
import com.mycc.game.common.Result;
import com.mycc.game.damin.Information;
import com.mycc.game.damin.Npc;
import com.mycc.game.damin.Scenario;
import com.mycc.game.service.NpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class NpcController {
    @Autowired
    private NpcService npcService;

    //查询所有怪谈信息
    @GetMapping("/selAllNpc/{pageNumber}/{pageSize}")
    public Result selNpcPage(@PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Npc> page = npcService.selAllNpc(pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //根据id查询怪谈信息
    @GetMapping("/selNpcById/{id}/{pageNumber}/{pageSize}")
    public Result selNpcById(@PathVariable Integer id, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Npc> page = npcService.selAllNpcById(id, pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //通过名字模糊查询怪谈信息
    @GetMapping("/selNpcByName/{name}/{pageNumber}/{pageSize}")
    public Result selNpcByName(@PathVariable String name, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Npc> page = npcService.selAllNpcByName(name, pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //删除npc信息
    @GetMapping("/delNpcById/{id}")
    public Result delNpcById(@PathVariable Integer id) {
        boolean flag = npcService.delNpc(id);
        if (flag) {
            return new Result(Code.DELETE_OK, "删除成功", flag);
        } else {
            return new Result(Code.DELETE_FAIL, "删除失败", null);
        }
    }

    //新值npc信息
    @PostMapping("/addNpc")
    public Result addNpc(@RequestBody Npc npc) {
        boolean flag = npcService.addNpc(npc);
        if (flag) {
            return new Result(Code.ADD_OK, "添加成功", flag);
        } else {
            return new Result(Code.ADD_FAIL, "添加失败", null);
        }
    }

    //修改npc信息
    @PostMapping("/updateNpc/{id}")
    public Result updateNpc(@PathVariable Integer id, @RequestBody Npc npc) {
        boolean flag = npcService.updNpc(id, npc);
        if (flag) {
            return new Result(Code.ADD_OK, "修改成功", flag);
        } else {
            return new Result(Code.ADD_FAIL, "修改失败", null);
        }
    }


}
